package com.gewu.pm.ui.adapter.funds;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.ManagementBean;
import com.hjq.base.BaseAdapter;

/**
 * 冰柜管理 适配器
 */
public class FreezerAdapter extends AppAdapter<ManagementBean> {

    private OnEditClickListener mOnEditClickListener;

    public FreezerAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public BaseAdapter<?>.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FreezerViewHolder();
    }

    /**
     * 设置编辑按钮点击监听
     */
    public void setOnEditClickListener(OnEditClickListener listener) {
        mOnEditClickListener = listener;
    }

    private final class FreezerViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTitleView;
        private final ImageView mEditButton;

        private FreezerViewHolder() {
            super(R.layout.freezer_item);
            mTitleView = findViewById(R.id.tv_title);
            mEditButton = findViewById(R.id.btn_edit);
            
            // 编辑按钮点击事件
            mEditButton.setOnClickListener(v -> {
                if (mOnEditClickListener != null) {
                    mOnEditClickListener.onEditClick(getViewHolderPosition(), getItem(getViewHolderPosition()));
                }
            });
        }

        @Override
        public void onBindView(int position) {
            ManagementBean item = getItem(position);
            mTitleView.setText(item.getName());
        }
    }

    /**
     * 编辑按钮点击监听接口
     */
    public interface OnEditClickListener {
        /**
         * 编辑按钮点击
         *
         * @param position 位置
         * @param item     数据项
         */
        void onEditClick(int position, ManagementBean item);
    }
}
